package cn.itcast.usermanage.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.abel533.entity.Example;
import com.github.abel533.entity.Example.Criteria;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.itcast.usermanage.mapper.NewUserMapper;
import cn.itcast.usermanage.pojo.EasyUIResult;
import cn.itcast.usermanage.pojo.User;

@Service
public class UserServiceImpl implements UserService{
	
	@Autowired
    private NewUserMapper userMapper;
	
	@Override
	public EasyUIResult queryEasyUIResult(Integer pageNum, Integer pageSize) {
		// 在查询方法调用之前，调用分页插件的静态方法，中间最好不要隔任何代码
		PageHelper.startPage(pageNum, pageSize);
		// 第一个参数是从那条开始，第二个参数是查询多少条
  //List<User> userList = this.userMapper.queryUserByPage((pageNum-1)*pageSize, pageSize);
	     List<User> userList = this.userMapper.select(null);
	 	// 初始化pageInfo对象，所有的分页参数都可以在该对象中获取
	      PageInfo<User> pageInfo = new PageInfo<>(userList);
		  EasyUIResult easyUIResult = new EasyUIResult();
		   easyUIResult.setTotal(pageInfo.getTotal());
		   easyUIResult.setRows(pageInfo.getList());
		return easyUIResult;
	}

	/*@Override
	public void addUser(User user1, User user2) {
	     this.userMapper.addUser(user1);
	 	// 制造异常
		//int i=1/0;
	     this.userMapper.addUser(user2);
	}*/

	@Override
	public Boolean insertUser(User user) {
	    int count = this.userMapper.insertSelective(user);
	    if(count==1){
	    	return true;
	    }
		return false;
	}

	@Override
	public boolean deleteUser(List<Object> ids) {
		    Example example = new Example(User.class);
	     Criteria criteria = example.createCriteria();
	     criteria.andIn("id", ids);
		int count = this.userMapper.deleteByExample(example);
		if(count>=1){
			return true;
		}
		return false;
	}

	@Override
	public void addUser(User user1, User user2) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean deleteUser(String[] ids) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public User queryUserById(Long id) {
		
		return this.userMapper.selectByPrimaryKey(id);
	}

	@Override
	public Boolean updateUser(User user) {
		return this.userMapper.updateByPrimaryKeySelective(user)>0;
		
	}

		
	

}
